//
//  UIView+Additions.m
//  MLIPhone
//
//  Created by yakehuang on 5/10/14.
//
//

#import "UIView+Additions.h"
static char *viewBackgroundColorGradientKey;
static const void *gradientLayerkey = &gradientLayerkey;

@implementation UIView (Additions)

- (void)setOriginX:(CGFloat)originX originY:(CGFloat)originY
{
    CGRect frame = self.frame;
    frame.origin = CGPointMake(originX, originY);
    self.frame = frame;
}

- (void)setOriginX:(CGFloat)originX
{
    CGRect frame = self.frame;
    frame.origin.x = originX;
    self.frame = frame;
}

- (void)setOriginY:(CGFloat)originY
{
    CGRect frame = self.frame;
    frame.origin.y = originY;
    self.frame = frame;
}

- (void)setWidth:(CGFloat)width height:(CGFloat)height
{
    CGRect originRect = self.frame;
    CGRect newRect = CGRectMake(originRect.origin.x, originRect.origin.y, width, height);
    self.frame = newRect;
}

- (void)setWidth:(CGFloat)width
{
    CGRect originRect = self.frame;
    CGRect newRect = CGRectMake(originRect.origin.x, originRect.origin.y, width, originRect.size.height);
    self.frame = newRect;
}

- (void)setHeight:(CGFloat)height
{
    CGRect originRect = self.frame;
    CGRect newRect = CGRectMake(originRect.origin.x, originRect.origin.y, originRect.size.width, height);
    self.frame = newRect;
}

- (void)removeAllSubViews
{
    for (UIView *v in [self subviews]) {
        [v removeFromSuperview];
    }
}

- (BOOL)findAndResignFirstResponder
{
    if (self.isFirstResponder)
    {
        [self resignFirstResponder];
        return YES;
    }
    
    for (UIView *subView in self.subviews)
    {
        if ([subView findAndResignFirstResponder])
        {
            return YES;
        }
    }
    
    return NO;
}

- (UIView *)findFirstResponder
{
    if (self.isFirstResponder)
    {
        return self;
    }
    
    for (UIView *subView in self.subviews)
    {
        UIView *responder = [subView findFirstResponder];
        if (responder)
        {
            return responder;
        }
    }
    
    return nil;
}

- (UIViewController*)viewController
{
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}



- (void)gradientColors:(NSArray*)colors Fram:(CGRect)fram cornerRadius:(CGFloat)radius{
    if (!self.gradientLayer) {
        CAGradientLayer *gradient = [[CAGradientLayer alloc]init];
        gradient.frame = CGRectMake(0, 0, fram.size.width, fram.size.height);
        gradient.startPoint = CGPointMake(0, 0);
        gradient.endPoint = CGPointMake(1, 0);
        gradient.colors =colors;
        gradient.cornerRadius = radius;
        [self.layer insertSublayer:gradient atIndex:0];
        self.gradientLayer = gradient;
    }
   
}

+ (void)drawDashLine:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:lineView.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}

- (UIImage *)convertViewToImage
{
    UIImage *imageRet = nil;
    UIGraphicsBeginImageContextWithOptions(self.frame.size, NO,  [UIScreen mainScreen].scale);
    //获取图像
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    imageRet = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageRet;
}

- (UIView*)subViewOfClassName:(NSString*)className {
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}

- (void)addTopCorners {
    [self addRoundedCorners:UIRectCornerTopLeft | UIRectCornerTopRight withRadius:CGSizeMake(10, 10)];
}
- (void)addBottomCorners {
    [self addRoundedCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight withRadius:CGSizeMake(10, 10)];
}
- (void)addRoundedCorners:(UIRectCorner)corners
                withRadius:(CGSize)radius {
    [self layoutIfNeeded];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01), dispatch_get_main_queue(), ^{
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: self.bounds byRoundingCorners:corners cornerRadii:radius];
        //创建 layer
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        //赋值
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    });
}

@end
